package com.lg.jvm.gc;

import java.util.concurrent.TimeUnit;

/**
 * 使用jstack 检测死锁
 *
 * @author Gang.Li
 * @date 2021/6/9 9:56
 */
public class DeadLockTest {
    public static final A lockA = new A();
    public static final B lockB = new B();

    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName() + " is Running");
        new Thread(() -> {
            try {
                getA();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "thread-1").start();
        new Thread(() -> {
            try {
                getB();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "thread-2").start();
    }

    public static void getA() throws InterruptedException {
        synchronized (lockA) {
            TimeUnit.SECONDS.sleep(5);
            synchronized (lockB) {
                System.out.println("我要获取B锁咯！！！");
            }
        }
    }

    public static void getB() throws InterruptedException {
        synchronized (lockB) {
            TimeUnit.SECONDS.sleep(5);
            synchronized (lockA) {
                System.out.println("我要获取A锁咯！！！");
            }
        }
    }

}

class A {

}

class B {

}
